#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "item.h"
#include <QDebug>
#include <QPainter>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //跟踪鼠标移动事件
    ui->centralWidget->setMouseTracking(true);
    setMouseTracking(true);
    ui->horizontalLayoutWidget->setMouseTracking(true);

    /**
    //设置图片
    QPixmap image1;
    image1.load(":/new/prefix1/45981451_p0.jpg");
    //image1 = image1.scaled(ui->label1->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    ui->label1->setPixmap(image1);
    QPixmap image2;
    image2.load(":/new/prefix1/43393846_p0.jpg");
    //image2 = image2.scaled(ui->label2->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    ui->label2->setPixmap(image2);
    */

    Item* item[20];
    int i = 0;
    for(i = 1; i <= (std::end(item) - std::begin(item)); i++){
        char tmpIndex[20];
        sprintf(tmpIndex, "label%d", i);
        QLabel *tmpLabel = ui->horizontalLayoutWidget->findChild<QLabel *>(tmpIndex, Qt::FindDirectChildrenOnly);
        if(tmpLabel != nullptr){
            tmpLabel->setMouseTracking(true);
            tmpLabel->setStyleSheet("QWidget{border:1px; "  //设置边框宽度
                                    "border-style:solid;" //设置边框风格
                           "border-color:red;"  //设置边框颜色
                                    "background: white;}"); //设置widget背景色

            item[i] = new Item(i, tmpLabel, this);
            //connect(this, SIGNAL(sendMouseEnterSig(int,bool)), item[i], SLOT(getMouseEnterSlot(int,bool)));
            connect(item[i], SIGNAL(setDescSig(const QString&)), tmpLabel, SLOT(setText(const QString&)));
            connect(this, SIGNAL(sendMouseMoveEventSig(QMouseEvent *)), item[i], SLOT(mouseMoveEventSlot(QMouseEvent *)));
        }
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    //qDebug() << "mouse move: " << event->pos();

    /*
    event->accept();

    if(enterQWidget(event->pos(), ui->label1)){
        //qDebug() << "label1";
        showDetail(ui->label1);
        emit sendMouseEnterSig(1, true);
    } else if (enterQWidget(event->pos(), ui->label2)) {
        //qDebug() << "label2";
        //showDetail(ui->label2);
        disableDetail(ui->label1);
        emit sendMouseEnterSig(2, true);
    }else{
        emit sendMouseEnterSig(1, false);
        disableDetail(ui->label1);
    }
    */
    emit sendMouseMoveEventSig(event);
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    //this->update();
}

bool MainWindow::enterQWidget(QPoint pp, QWidget* qWidget)
{
    int height = qWidget->height();
    int width = qWidget->width();

    QPoint widgetPos = getPosInMainWindow(qWidget);

    QPoint widgetMinPos = widgetPos;
    QPoint widgetMaxPos = widgetPos;

    widgetMaxPos.setX(widgetPos.x() + width);
    widgetMaxPos.setY(widgetPos.y() + height);

    if(pp.x() >= widgetMinPos.x() && pp.y() >= widgetMinPos.y()
            && pp.x() <= widgetMaxPos.x() && pp.y() <= widgetMaxPos.y()){
        return true;
    }
    else {
        return false;
    }
}

QPoint MainWindow::getPosInMainWindow(QWidget *qWidget)
{
    QPoint posInMainWindow = qWidget->pos();

    QWidget* qParent = qWidget->parentWidget();
    int max = 10;
    while(QString::compare(qParent->objectName(), "MainWindow")){
        posInMainWindow = posInMainWindow + qParent->pos();
        //qDebug() << "parentWidget: " << parentWidget->objectName() << "   posInMainWindow: " << posInMainWindow;
        qParent = qParent->parentWidget();

        if(max > 0){
            max--;
        }else{
            break;
        }
    }

    return posInMainWindow;
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPoint label1Pos = getPosInMainWindow(ui->label1);
    //设置画笔
    QPainter painter(this);
    QPen pen;

    //画中心
    QRectF rectangleEllipse(label1Pos.x(), label1Pos.y(), 10, 10);
    QRectF rectangleRect(label1Pos.x(), label1Pos.y(), ui->label1->width(), ui->label1->height());
    pen.setColor(Qt::blue);
    painter.setPen(pen);
    painter.drawEllipse(rectangleEllipse);
    painter.drawRect(rectangleRect);

    getPosInMainWindow(ui->label1);
}

void MainWindow::showDetail(QWidget *qWidget)
{
    QLabel *qDetail = this->findChild<QLabel *>(qWidget->objectName() + "_detailLabel", Qt::FindDirectChildrenOnly);
    if(qDetail != nullptr){
        //qDebug() << "find " << qDetail->objectName();
        qDetail->setVisible(true);
    } else {
        QLabel *qLabelDetail = new QLabel("detailLabel", this);
        QPoint qWidgetPos = getPosInMainWindow(qWidget);
        qLabelDetail->setText(qWidget->objectName() + ":detail");
        qLabelDetail->setParent(this);
        qLabelDetail->setObjectName(qWidget->objectName() + "_detailLabel");
        qLabelDetail->setGeometry(qWidgetPos.x() + qWidget->width(), qWidgetPos.y(), qWidget->width(), qWidget->height() * 2);
        qLabelDetail->setStyleSheet("QWidget{border:1px; "  //设置边框宽度
                                "border-style:solid;" //设置边框风格
                       "border-color:green;"  //设置边框颜色
                                "background: white;}"); //设置widget背景色
        qLabelDetail->setVisible(true);
        qLabelDetail->show();
        //qDebug() << "new " << qLabelDetail->objectName();
    }
}

void MainWindow::disableDetail(QWidget *qWidget)
{
    QLabel *qDetail = this->findChild<QLabel *>(qWidget->objectName() + "_detailLabel", Qt::FindDirectChildrenOnly);
    if(qDetail != nullptr){
        //qDebug() << "find " << qDetail->objectName();
        qDetail->setVisible(false);
    }

}



